
package cn.wjdiankong.levideo.fragment;

import com.yixia.utils.TinyEncode;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.list.adapter.MiaopaiFragmentListAdapter;
import cn.wjdiankong.levideo.miaopai.data.MiaopaiVideoListData;
import cn.wjdiankong.levideo.miaopai.utils.MiaopaiUtils;
import cn.wjdiankong.levideo.net.HttpClientUtils;
import cn.wjdiankong.levideo.net.HttpClientUtils.ResponseCallback;
import cn.wjdiankong.levideo.utils.Logger;

public class MiaopaiFragment extends BaseFragment{

	private static final String ARG_POSITION = "position";
	
	private int page = 0;
	private long updateTime = 0;
	
	public static MiaopaiFragment newInstance(int position, String title, int type) {
		MiaopaiFragment f = new MiaopaiFragment();
		f.title = title;
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		f.type = type;
		return f;
	}
	
	@SuppressLint({ "InlinedApi", "InflateParams", "NewApi" })
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View layout = super.onCreateView(inflater, container, savedInstanceState);
		mListView = (ListView) layout.findViewById(R.id.listview);
		adapter = new MiaopaiFragmentListAdapter(getActivity(), mListView, isLocal);
		adapter.setListView(mListView);
		initListViewListener();
		if(!isLocal){
			showLoading();
			getBeforeListData();
		}else{
			if(adapter != null){
				adapter.setBeforeDataList(videoDataList);
			}
		}
		return layout;
	}
	
	public void getBeforeListData(){
		long time = System.currentTimeMillis()/1000;
		updateTime = time;
		String url = MiaopaiUtils.getUrl(getActivity(), page > 0 ? 1 : 0, updateTime, time);
		HttpClientUtils httpClient = new HttpClientUtils();
		httpClient.setUrl(url).setResponseByte(true).setHeaderParams(
				MiaopaiUtils.getHeaderParams(time)).setResponseCallback(
				new ResponseCallback() {
			@Override
			public void responseFinish(final byte[] result) {
				String resultStr = TinyEncode.DecodeResult(result);
				MiaopaiVideoListData listData = MiaopaiVideoListData.fromJSONData(resultStr);
				loadDownDataFinish(listData.videoDataList);
			}
			
			@Override
			public void responseFinish(String result) {
				
			}
			
			@Override
			public void reponseFail() {
				Logger.log("get data err!");
				loadDataFailed();
			}
		}).execGet();
		page = 1;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_miaopai_layout;
	}

	@Override
	protected void getAfterListData() {
		long time = System.currentTimeMillis()/1000;
		String url = MiaopaiUtils.getUrl(getActivity(), page, updateTime, time);
		HttpClientUtils httpClient = new HttpClientUtils();
		httpClient.setUrl(url).setResponseByte(true).setHeaderParams(
				MiaopaiUtils.getHeaderParams(time)).setResponseCallback(
				new ResponseCallback() {
			@Override
			public void responseFinish(final byte[] result) {
				String resultStr = TinyEncode.DecodeResult(result);
				MiaopaiVideoListData listData = MiaopaiVideoListData.fromJSONData(resultStr);
				loadUpDataFinish(listData.videoDataList);
			}
			
			@Override
			public void responseFinish(String result) {
				
			}
			
			@Override
			public void reponseFail() {
				Logger.log("get data err!");
				loadDataFailed();
			}
		}).execGet();
		page++;
		updateTime = time;
	}

}